package it.uniba.di.mas.jpnde.core;


import jade.util.leap.HashMap;
import jade.util.leap.Map;
import jade.util.leap.Serializable;

public class Token 
	implements Serializable,Cloneable{
	private static final long serialVersionUID = 1L;
	
	private String id="";
	private String placeId="";
	private String source="";
	private PlaceEvent event=null;
	private Map attrsMap=new HashMap();
	
	public Token(String source)
	{
		this.source=source;
	}
	
	public Token(String source,PlaceEvent event)
	{
		this.source=source;
		this.event=event;
	}
	
	public void setId(String id){
		this.id=id;
	}
	
	public String getId(){
		return id;
	}
	
	public String getPlaceId()
		{return placeId;}
	
	public void setPlaceId(String placeId)
		{this.placeId=placeId;}
	
	public String getSource()
	{return source;}
	
	public PlaceEvent getEvent() {
		return event;
	}

	public void setEvent(PlaceEvent event) {
		this.event = event;
	}

	public void clearAttributes(){
		attrsMap.clear();
	}
	
	public Map getAttributes(){
		return attrsMap;
	}
	
	public void put(String key,Object value){
		attrsMap.put(key, value);
	}
	
	public Object get(String key){
		return attrsMap.get(key);
	}
	
	public void remove(String key){
		attrsMap.remove(key);
	}
	
	public boolean equals(Object obj){
		if (!(obj instanceof Token))
			return false;
		Token t=(Token)obj;
		if (!t.getId().equals(id))
			return false;
		else
			return true;
	}	
	
	@Override
	public String toString() {
		return "Token [id="+id+";attributes="+attrsMap+";source=" + source+";placeId=" + placeId + "]";
	}
	
	public Object clone()
	{
		Token ret=new Token(source);
		ret.setId(id);
		ret.setPlaceId(placeId);
		ret.setEvent(event);
		
		Object[] values=attrsMap.values().toArray();
		Object[] keys=attrsMap.keySet().toArray();
		for(int i=0;i<keys.length;i++){
			ret.put((String)keys[i], values[i]);
		}
		return ret;
	}
}
